package com.mk.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mk.springboot.mapper.CategoryMapper;
import com.mk.springboot.mapper.PlateMapper;
import com.mk.springboot.pojo.Category;
import com.mk.springboot.pojo.Plate;
import com.mk.springboot.service.CategoryService;
import com.mk.springboot.until.PageFactory;
import com.mk.springboot.until.Result;
import com.mk.springboot.until.ResultUtil;
import com.mk.springboot.vo.CategoryPlateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CategoryController {
    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    PlateMapper plateMapper;

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/category/show")
    public String show(Model model) {
        List<Plate> plates = plateMapper.selectList(null);
        model.addAttribute("plates", plates);
        return "/category/list";
    }

    @RequestMapping("/category/list")
    @ResponseBody
    public PageFactory list(HttpServletRequest httpServletRequest) {
        String name = httpServletRequest.getParameter("name");
        String plateIdString = httpServletRequest.getParameter("plateId");
        Integer plateId = plateIdString != null && plateIdString != "" ? Integer.valueOf(plateIdString) : null;
        Integer start = Integer.parseInt(httpServletRequest.getParameter("start"));
        Integer size = Integer.parseInt(httpServletRequest.getParameter("length"));
        Integer current = start / size + 1;

        Page<CategoryPlateVO> pageParam = new Page<>(current, size);// 当前页，总条数 构造 page 对象
        Page<CategoryPlateVO> page = categoryService.getCategoryPlate(pageParam, name, plateId);

        PageFactory pageFac = new PageFactory();
        pageFac.setRecordsFiltered(page.getTotal());
        pageFac.setRecordsTotal(page.getTotal());
        pageFac.setData(page.getRecords());

        return pageFac;
    }

    @GetMapping("/category/edit")
    public String edit(Model model, HttpServletRequest httpServletRequest) {
        String id = httpServletRequest.getParameter("id");
        Category category = categoryMapper.selectById(Integer.parseInt(id));
        model.addAttribute("category", category);
        return "/category/edit";
    }

    @PostMapping("/category/edit")
    @ResponseBody
    public Result edit(Category category) {
        categoryMapper.updateById(category);
        return ResultUtil.success("成功修改分类: " + category.getName() + " 信息!");
    }

    @GetMapping("/category/add")
    public String add(Model model) {
        List<Plate> plates = plateMapper.selectList(null);
        model.addAttribute("plates", plates);
        return "/category/add";
    }

    @PostMapping("/category/add")
    @ResponseBody
    public Result add(Category category) {
        categoryMapper.insert(category);
        return ResultUtil.success("成功添加分类：" + category.getName() + "信息!");
    }

    @GetMapping("/category/del")
    @ResponseBody
    public Result del(HttpServletRequest httpServletRequest) {
        String id = httpServletRequest.getParameter("id");
        categoryMapper.deleteById(Integer.parseInt(id));
        return ResultUtil.success("成功删除用户信息!");
    }
}
